﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Joyintech.IDAL.inpiao365;
using Joyintech.DBUtility;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Joyintech.Model.inpiao365;

namespace Joyintech.SQL2008DAL.inpiao365
{
    public partial class order_DAL : AbstractDAL, Iorder_DAL
    {
        /// <summary>
        /// 获得分页订单列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetOrderList(string strWhere, int currentPage)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select * from (");
            strSql.Append("select ROW_NUMBER() OVER(ORDER BY OrderTime DESC) AS 'Row',CustomerId,a.MovieId,a.TheaterId,a.TheaterRoomId,a.ScreeningsId,OrderId,OrderTime,b.MovieLogo,b.MovieName,d.TheaterName,c.MovieSeatTime,a.RecMobile,c.Price,e.TheaterRoomName,MovieSeatId,OrderState,a.TotalMoney ");
            strSql.Append(" FROM [order] a,movie b,screenings c ,theater d,theaterRoom e where a.movieId=b.movieId and a.ScreeningsId=c.ScreeningsId and a.TheaterRoomId= e.TheaterRoomId and a.TheaterId=d.TheaterId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" ) t where Row>=" + (currentPage - 1) * 8 + " and Row<=" + currentPage * 8);
            Database db = this.CreateDatabase();
            return ExecuteDataSet(db, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得订单列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetOrderList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select CustomerId,a.MovieId,a.TheaterId,a.TheaterRoomId,a.ScreeningsId,OrderId,OrderTime,b.MovieLogo,b.MovieName,d.TheaterName,c.MovieSeatTime,a.RecMobile,c.Price,e.TheaterRoomName,MovieSeatId,OrderState,a.TotalMoney ");
            strSql.Append(" FROM [order] a,movie b,screenings c ,theater d,theaterRoom e where a.movieId=b.movieId and a.ScreeningsId=c.ScreeningsId and a.TheaterRoomId= e.TheaterRoomId and a.TheaterId=d.TheaterId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }

            Database db = this.CreateDatabase();
            return ExecuteDataSet(db, CommandType.Text, strSql.ToString());
        }



        /// <summary>
        ///  获取与指定订单关联的信息
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public DataSet GetFullModel(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CustomerId,a.MovieId,a.TheaterId,a.TheaterRoomId,a.ScreeningsId,TotalMoney,OrderId,OrderTime,b.MovieLogo,b.MovieLogo,b.MovieType,b.MovieName,d.TheaterName,c.MovieSeatTime,a.RecMobile,c.Price,e.TheaterRoomName,MovieSeatId,OrderState,a.TotalMoney ");
            strSql.Append(" FROM [order] a,movie b,screenings c ,theater d,theaterRoom e where a.movieId=b.movieId and a.ScreeningsId=c.ScreeningsId and a.TheaterRoomId= e.TheaterRoomId and a.TheaterId=d.TheaterId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            Database db = this.CreateDatabase();
            return ExecuteDataSet(db, CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得查询行数
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int RecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(orderid) ");
            strSql.Append(" FROM [order]  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = this.CreateDatabase();
            return (int)ExecuteScalar(db, CommandType.Text, strSql.ToString());
        }
    }
}
